package com.muqi.server.common.auth;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;

import java.util.Date;

public class DoubleToken {
    private static final String ACCESS_TOKEN_KEY = "MUQI_access_token";
    private static final String REFRESH_TOKEN_KEY = "MUQI_refresh_token";
    private static final long ACCESS_TOKEN_EXPIRATION_TIME = 60 * 15 * 1000;// 15分钟
    private static final long REFRESH_TOKEN_EXPIRATION_TIME = 60 * 60 * 24 * 7 * 1000;// 7天

    public static String getAccessToken(Integer ID,Integer type) {
        Date now = new Date();
        Date expiryDate = new Date(now.getTime() + ACCESS_TOKEN_EXPIRATION_TIME);
        return JWT.create()
                .withClaim("ID", ID)
                .withClaim("type", type)
                .withExpiresAt(expiryDate)
                .sign(Algorithm.HMAC256(ACCESS_TOKEN_KEY));
    }

    public static String getRefreshToken(Integer ID,Integer type, Integer count) {
        Date now = new Date();
        Date expiryDate = new Date(now.getTime() + REFRESH_TOKEN_EXPIRATION_TIME);
        return JWT.create()
                .withClaim("ID", ID)
                .withClaim("type", type)
                .withClaim("count", count)
                .withExpiresAt(expiryDate)
                .sign(Algorithm.HMAC256(REFRESH_TOKEN_KEY));
    }


    public static boolean verifyAccessToken(String token) {
        try {
            JWT.require(Algorithm.HMAC256(ACCESS_TOKEN_KEY)).build().verify(token);
            return getID(token) != -1 && getType(token) != -1;
        } catch (Exception e) {
            return false;
        }
    }

    // 验证token是否正确和过期
    public static boolean verifyRefreshToken(String token) {
        try {
            JWT.require(Algorithm.HMAC256(REFRESH_TOKEN_KEY)).build().verify(token);
            return getID(token) != -1 && getType(token) != -1;
        } catch (Exception e) {
            return false;
        }
    }

    // 获取token中的ID
    public static Integer getID(String token) {
        Claim claim = JWT.decode(token).getClaim("ID");
        if (claim.isNull()) {
            return -1;
        }
        return claim.asInt();
    }

    // 获取token中的type
    public static Integer getType(String token) {
        Claim claim = JWT.decode(token).getClaim("type");
        if (claim.isNull()) {
            return -1; // 或者其他默认值
        }
        return claim.asInt();
    }

    public static Integer getCount(String token) {
        Claim claim = JWT.decode(token).getClaim("count");
        if (claim.isNull()) {
            return -1; // 或者其他默认值
        }
        return claim.asInt();
    }

    //判断refreshToken 还多久过期
    //如果剩余时间小于1/4  则返回真

    public static boolean isNeedRefresh(String token) {
        long remainingTime = getRemainingTime(token);
        if (remainingTime < 0) {
            return false;
        }
        return getRemainingTime(token) < REFRESH_TOKEN_EXPIRATION_TIME / 4;
    }

    public static long getRemainingTime(String token) {
        Date date = JWT.decode(token).getExpiresAt();
        return date.getTime() - new Date().getTime();
    }

}
